#pragma once

// Ownership Policy: reference counting

template <class P>	// PointerType
class RefCounted
{
public:
	RefCounted()
	{
		mCounter = new unsigned int(1);	// TODO: handle exception + use allocator
	}

	RefCounted(const RefCounted& rhs) :		// doesn't increment, user should do it with Clone
	  mCounter(rhs.mCounter)
	{
	}

	template <class P1>
	RefCounted(const RefCounted<P1>& rhs) :	// doesn't increment, user should do it with Clone
	  mCounter(rhs.mCounter)
	{
	}

	P Clone(const P& iVal) 
	{
		++*mCounter;
		return iVal;
	}

	bool Release(const P&)
	{
		if(!--*mCounter)
		{
			MM_DELETE ( mCounter );
			return true;
		}
		return false;
	}

	void Swap(RefCounted &rhs)
	{
		std::swap(mCounter, rhs.mCounter);
	}

	enum { destructiveCopy = false };

private:
	unsigned int *mCounter;
};